package com.esri.core.geometry;

/* loaded from: classes.dex */
class IndexHashTable {
    int a;
    AttributeStreamOfInt32 b;
    IndexMultiList c = new IndexMultiList();
    HashFunction d;

    /* loaded from: classes.dex */
    public static abstract class HashFunction {
        public abstract boolean equal(int i, int i2);

        public abstract boolean equal(Object obj, int i);

        public abstract int getHash(int i);

        public abstract int getHash(Object obj);
    }

    public IndexHashTable(int i, HashFunction hashFunction) {
        this.b = new AttributeStreamOfInt32(i, nullNode());
        this.d = hashFunction;
    }

    public void addElement(int i) {
        int hash = this.d.getHash(i) % this.b.size();
        int i2 = this.b.get(hash);
        if (i2 == IndexMultiList.nullNode()) {
            i2 = this.c.createList();
            this.b.set(hash, i2);
        }
        this.c.addElement(i2, i);
    }

    public void deleteElement(int i) {
        int hash = this.d.getHash(i) % this.b.size();
        int i2 = this.b.get(hash);
        if (i2 == IndexMultiList.nullNode()) {
            throw new IllegalArgumentException();
        }
        int first = this.c.getFirst(i2);
        int nullNode = IndexMultiList.nullNode();
        while (first != IndexMultiList.nullNode()) {
            int element = this.c.getElement(first);
            int next = this.c.getNext(first);
            if (element == i) {
                this.c.deleteElement(i2, nullNode, first);
                if (this.c.getFirst(i2) == IndexMultiList.nullNode()) {
                    this.c.deleteList(i2);
                    this.b.set(hash, IndexMultiList.nullNode());
                }
            } else {
                nullNode = first;
            }
            first = next;
        }
    }

    public int getFirstInBucket(int i) {
        int i2 = this.b.get(i % this.b.size());
        if (i2 == IndexMultiList.nullNode()) {
            return IndexMultiList.nullNode();
        }
        return this.c.getFirst(i2);
    }

    public int getNextInBucket(int i) {
        return this.c.getNext(i);
    }

    public int findNode(int i) {
        int i2 = this.b.get(this.d.getHash(i) % this.b.size());
        if (i2 == IndexMultiList.nullNode()) {
            return IndexMultiList.nullNode();
        }
        int first = this.c.getFirst(i2);
        while (first != IndexMultiList.nullNode()) {
            if (this.d.equal(this.c.getElement(first), i)) {
                return first;
            }
            first = this.c.getNext(first);
        }
        return IndexMultiList.nullNode();
    }

    public int findNode(Object obj) {
        int i = this.b.get(this.d.getHash(obj) % this.b.size());
        if (i == IndexMultiList.nullNode()) {
            return IndexMultiList.nullNode();
        }
        int first = this.c.getFirst(i);
        while (first != IndexMultiList.nullNode()) {
            if (this.d.equal(obj, this.c.getElement(first))) {
                return first;
            }
            first = this.c.getNext(first);
        }
        return IndexMultiList.nullNode();
    }

    public int getNextNode(int i) {
        int element = this.c.getElement(i);
        int next = this.c.getNext(i);
        while (next != IndexMultiList.nullNode()) {
            if (this.d.equal(this.c.getElement(next), element)) {
                return next;
            }
            next = this.c.getNext(next);
        }
        return IndexMultiList.nullNode();
    }

    public void deleteNode(int i) {
        int hash = this.d.getHash(getElement(i)) % this.b.size();
        int i2 = this.b.get(hash);
        if (i2 == IndexMultiList.nullNode()) {
            throw new IllegalArgumentException();
        }
        int first = this.c.getFirst(i2);
        int nullNode = IndexMultiList.nullNode();
        while (first != IndexMultiList.nullNode()) {
            if (first == i) {
                this.c.deleteElement(i2, nullNode, first);
                if (this.c.getFirst(i2) == IndexMultiList.nullNode()) {
                    this.c.deleteList(i2);
                    this.b.set(hash, IndexMultiList.nullNode());
                    return;
                }
                return;
            }
            nullNode = first;
            first = this.c.getNext(first);
        }
        throw new IllegalArgumentException();
    }

    public int getElement(int i) {
        return this.c.getElement(i);
    }

    public int getAnyElement() {
        IndexMultiList indexMultiList = this.c;
        return indexMultiList.getFirstElement(indexMultiList.getFirstList());
    }

    public int getAnyNode() {
        IndexMultiList indexMultiList = this.c;
        return indexMultiList.getFirst(indexMultiList.getFirstList());
    }

    public static int nullNode() {
        return IndexMultiList.nullNode();
    }

    public void clear() {
        this.b = new AttributeStreamOfInt32(this.b.size(), nullNode());
        this.c.clear();
    }

    public int size() {
        return this.c.getNodeCount();
    }
}
